package haoyc.server;

import haoyc.entity.Message;
import haoyc.utils.GzipUtils;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * @author Memory
 * @version 1.0
 * @date 2021/11/23
 */
@Slf4j
public class RequestMessage {

    private String local;
    private String name;
    private Message message;

    public RequestMessage(String local, String name) {
        this.local = local;
        this.name = name;
        this.message = init();
    }

    public Message init(){
        Message message = new Message();
        message.setSender(local);
        message.setReceiver(name);
        return message;
    }

    public Message sendMessages() {
        this.message.setAttachment(getPicture());
        return message;
    }

    private byte[] getPicture() {
        byte[] data = new byte[0];
        try {
            BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(screenshot, "png", out);
            data = out.toByteArray();
            data = GzipUtils.gzip(data);
            out.close();
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}
